package tt.dz.service.imp.base;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.JsonPageForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.util.OmsConstant.MsgCodeEnum;
import tt.dz.util.RedisClient;

/**
 * 公共类
 * @author huanghao
 *
 */

public class BaseService 
{
	
	private  Log log = LogFactory.getLog(OmsCommonDao.class); 
	
	private RedisTemplate<String, String> redisTemplate;
	
	public void logError(String errorMsg,Throwable error)
	{
		log.error(errorMsg, error);
	}
	
	public void logDebug(String msg)
	{
		log.debug(msg);
	}
	
	protected <T> JsonForList<T> buildSuccessJsonForList(OmsCommonParam param,List<T> list,int total)
	{
		JsonForList<T> jfl = null;
		if (param != null && param.isQueryTotal())
		{
			jfl = new JsonPageForList<T>();
			((JsonPageForList<T>)jfl).setTotal(total);
		}
		else
		{
			jfl = new JsonForList<T>();
		}
		jfl.setList(list);
		jfl.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
		jfl.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
		return jfl;
	}
	
	protected <T> JsonForList<T> buildSuccessJsonForList(List<T> list)
	{
		JsonForList<T> jfl =  new JsonForList<T>();
		jfl.setList(list);
		jfl.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
		jfl.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
		return jfl;
	}
	
	protected <T> JsonCommon<T> buildSuccessJsonCommon(T obj)
	{
		JsonCommon<T> jfl = new JsonCommon<>();
		jfl.setPojo(obj);
		jfl.setCode(MsgCodeEnum.SUCCESS.getMsgCode());
		jfl.setMsg(MsgCodeEnum.SUCCESS.getMsgInfo());
		return jfl;
	}
	
	protected <T> JsonCommon<T> buildError400JsonCommon()
	{
		JsonCommon<T> jfl = new JsonCommon<>();
		jfl.setCode(MsgCodeEnum.ERROR400.getMsgCode());
		jfl.setMsg(MsgCodeEnum.ERROR400.getMsgInfo());
		return jfl;
	}
	
	protected <T> JsonCommon<T> buildOtherJsonCommon(String msgCode,String msg)
	{
		JsonCommon<T> jfl = new JsonCommon<>();
		jfl.setCode(msgCode);
		jfl.setMsg(msg);
		return jfl;
	}
	
	public <T> JsonForList<T> buildError400JsonForList()
	{
		JsonForList<T> jfl = new JsonForList<>();
		jfl.setCode(MsgCodeEnum.ERROR400.getMsgCode());
		jfl.setMsg(MsgCodeEnum.ERROR400.getMsgInfo());
		return jfl;
	}
	
	public JsonForList<?> buildOtherErrorJsonForList(String msgCode,String msg)
	{
		JsonForList<?> jfl = new JsonForList<>();
		jfl.setCode(msgCode);
		jfl.setMsg(msg);
		return jfl;
	}
	
	
	protected  String genarateId(String maxId,int idLength) throws OmsDaoException
	{
		String lastIdStr = "0";
		if (StringUtils.isNotEmpty(maxId)  &&  maxId.startsWith("0"))
		{
			lastIdStr = maxId.replaceFirst("^0*", "");
		}
		else if (maxId != null)
		{
			lastIdStr = maxId;
		}
		if (StringUtils.isBlank(lastIdStr)) {
			lastIdStr = "0";
		}
		
		if (StringUtils.isNumeric(lastIdStr))
		{
			Long idNum = Long.parseLong(lastIdStr)+1;
			if (idLength <= 0)
			{
				idLength = 8;
			}
			StringBuilder formatSb = new StringBuilder();
			formatSb.append("%0").append(idLength).append("d");
			return String.format(formatSb.toString(), idNum);
		}
		return null;
		
	}
	
	protected List<String> getRedisCache(String sbKey)
	{
		/*try 
		{
			if (redisTemplate  == null)
			{
				return null;
			}
			ListOperations<String, String> opsLst = redisTemplate.opsForList();
			Long size = opsLst.size(sbKey.toString());
			if (size != null && size > 0)
			{
				return opsLst.range(sbKey, 0, size);
			}
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
		}
		return null;*/
		return RedisClient.getList(sbKey);
	}
	
	protected void addRedisCache(String sbKey,List<String> values,long timeOut)
	{
		/*try 
		{
			if (redisTemplate  == null)
			{
				return;
			}
			ListOperations<String, String> opsLst = redisTemplate.opsForList();
			redisTemplate.delete(sbKey.toString());
			if (values != null && values.size() > 0)
			{
				opsLst.leftPushAll(sbKey, values.toArray(new String[values.size()]));
				redisTemplate.expire(sbKey, timeOut, TimeUnit.DAYS);
			}
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
		}*/
		RedisClient.delByKey(sbKey.toString());
		if (values != null && values.size() > 0)
		{
			RedisClient.putList(sbKey, values);
			RedisClient.expire(sbKey, (int)(timeOut*24*60*60));
		}
	}

	public RedisTemplate<String, String> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
	
	
}
